package com.uenpay.im.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aswife.ui.MaskImageView;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.SelectedFriendEntity;

import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class PickFriendsAdapter extends BaseAdapter {

	private ArrayList<SelectedFriendEntity> list;
	private LayoutInflater mInflater;

	public PickFriendsAdapter(Context _mContext,
			ArrayList<SelectedFriendEntity> _list) {
		mInflater = LayoutInflater.from(_mContext);
		list = _list;
	}

	public void setData(ArrayList<SelectedFriendEntity> _list) {
		list = _list;
		notifyDataSetChanged();
	}

	public void setSelectUser(String custId, int _selectedStatus) {
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).friend.cust_id.equals(custId)) {
				list.get(i).selectedStatus = _selectedStatus;
				break;
			}
		}
		// list.get(index).isSelected = is_selected;
		notifyDataSetChanged();
	}

	public int isSelected(String custId) {
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).friend.cust_id.equals(custId)) {
				return list.get(i).selectedStatus;
			}
		}
		return 1;
	}

	@Override
	public int getCount() {
		return list == null ? 0 : list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return list == null ? null : list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return list == null ? 0 : arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		PickFriendsViewHolder holder = null;
		SelectedFriendEntity entity = list.get(arg0);
		if (arg1 == null) {
			arg1 = mInflater.inflate(R.layout.pick_friends_list_item_layout,
					null);
			holder = new PickFriendsViewHolder();
			holder.avatar = (MaskImageView) arg1
					.findViewById(R.id.friend_avatar_iv);
			holder.seleced_friend_iv = (ImageView) arg1
					.findViewById(R.id.selected_friend_iv);
			holder.name = (TextView) arg1.findViewById(R.id.friend_name_tv);
			arg1.setTag(holder);
		} else {
			holder = (PickFriendsViewHolder) arg1.getTag();
		}
		if(!TextUtils.isEmpty(entity.friend.remarks_name)){
			holder.name.setText(entity.friend.remarks_name);
		}else{
			holder.name.setText(entity.friend.nick_name);
		}
		holder.avatar.SetUrl(APIConfiguration.getAvatarUrlNormal(
				entity.friend.cust_id, PacketType.PERSONAL));
		if (entity.selectedStatus == 2) {
			holder.seleced_friend_iv.setImageResource(R.drawable.icon_checked);
		} else if (entity.selectedStatus == 1) {
			holder.seleced_friend_iv.setImageResource(R.drawable.icon_uncheck);
		} else {
			holder.seleced_friend_iv.setImageResource(R.drawable.icon_gray);
		}
		return arg1;
	}

	private class PickFriendsViewHolder {
		private ImageView seleced_friend_iv;
		private MaskImageView avatar;
		private TextView name;
	}

}
